import request from './request';
import qs from 'qs';
const thridPartUrl = `http://wccg.tech:88`;
const newThridPartUrl = 'http://wccg.tech:82';

// 设置基本的opthions
function setPostOptions(param) {
  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  };
  if (param) {
    options.body = param.toString();
  }
  return options;
}

// 检查token是否正常
export function checkToken(token) {
  const query = `?${qs.stringify({ token })}`;
  return request(thridPartUrl + '/api/smart/validate' + query, { method: 'GET' }).then(data => {
    return data.data;
  });
}

// 查询出所有的项目信息
export function findProjectInfo() {
  const formData = new URLSearchParams();
  formData.append('token', window.appcfg.kanbanToken);
  const options = setPostOptions(formData.toString());
  // return request(thridPartUrl + '/api/trans/project/info', options);
  return request(newThridPartUrl + '/api/trans/project/allProject?emphasisFlag=1', options);
}

// 查询出项目有参见单位的单位信息
export function findProjectCorpInfo() {
  const formData = new URLSearchParams();
  formData.append('token', window.appcfg.kanbanToken);
  const options = setPostOptions('POST', { token: window.appcfg.kanbanToken });
  return request(window.appcfg.kanbanUrl + '/projectcorp/findProjectCorpInfo', options);
}

// 查询出项目在岗人员基础信息
export function findUserInfo(projectCode) {
  const query = `?${qs.stringify({
    // todo
    token: 'ac1a80d836fad59eca9b4d85eebb3dfb',
    projectCode,
    // projectId: projectCode,
  })}`;

  return request(window.appcfg.kanbanUrl + '/user/findUserInfo' + query, { method: 'GET' });
}

// 查询项目进度
export function getProjProgress(projectId) {
  const query = `?${qs.stringify({ token: window.appcfg.kanbanToken })}`;
  return request(thridPartUrl + '/api/wuxi/api/project/progress/' + projectId + query, {
    method: 'GET',
  });
}

// 查询两个计划开始时间内的时间节点内项目进度列表
export function getProjProgressBetween({ endDateTime, projectId, startDateTime }) {
  // const query = `?${qs.stringify({ token: window.appcfg.kanbanToken })}`;
  return request(
    thridPartUrl + '/api/wuxi/api' + '/project/progress/between/date',
    //  + query,
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        endDateTime,
        projectId,
        startDateTime,
      }),
    },
  );
}
// 查询两个计划开始时间内的时间节点内项目进度列表
export function getProjProgressBeforePlan({ dateTime, projectId }) {
  // const query = `?${qs.stringify({ token: window.appcfg.kanbanToken })}`;
  return request(
    thridPartUrl + '/api/wuxi/api' + '/project/progress/less/planEndDate',
    //  + query,
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        dateTime,
        projectId,
      }),
    },
  );
}

// 查询所有的公司以及公司下进行的项目信息
export function getCompanys() {
  return request(thridPartUrl + '/api/wuxi/api' + '/project/companys', { method: 'GET' });
}

// 查询两个计划开始时间内的时间节点内项目进度列表
export function getProjProgressBeforeActual({ dateTime, projectId }) {
  // const query = `?${qs.stringify({ token: window.appcfg.kanbanToken })}`;
  return request(
    thridPartUrl + '/api/wuxi/api' + '/project/progress/less/actualEndDate',
    //  + query,
    {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        dateTime,
        projectId,
      }),
    },
  );
}

/**
 * 梁场相关api
 */

// 预制梁场系统接口
let loginPromiseCache;
function getBridgeYardToken() {
  let token = sessionStorage.getItem('bridgeYardLogin');
  if (token) return Promise.resolve(token);
  if (loginPromiseCache) return loginPromiseCache;
  loginPromiseCache = bridgeYardLogin();
  return loginPromiseCache;
}

// 梁场登录
function bridgeYardLogin() {
  return request(
    'http://81.68.105.79:59463/api' + '/Login/CheckLogin?account=tantaiwu&password=123456',
    { method: 'GET' },
  ).then(data => {
    console.log(data);
    sessionStorage.setItem('bridgeYardLogin', data.resultdata || '');
    return data.resultdata;
  });
}

// 梁场生产进度
export async function getBridgeProgress({ year = 2020 }) {
  const token = await getBridgeYardToken();
  const query = `?${qs.stringify({ token, year })}`;
  return request('http://81.68.105.79:59463/api' + '/BridgeYard/SumBridgeProgress' + query, {
    method: 'GET',
  });
}

// 台座统计
export async function getPedestalTotal() {
  const token = await getBridgeYardToken();
  const query = `?${qs.stringify({ token })}`;
  return request('http://81.68.105.79:59463/api' + '/BridgeYard/SumPedestalTotal' + query, {
    method: 'GET',
  });
}

// 获取全部项目和模型的关系
export async function getProjandModelNum() {
  return request(window.appcfg._servicesUrl + '/api/projects', {
    method: 'GET',
  }).then(({ data }) => {
    return data.map(({ code, modelList, name }) => {
      return { code, name, modelNum: modelList.length };
    });
  });
}

/**
 * 工控系统接口api
 */

// 获取工控系统接口信息
export async function getIndustrialSystemInfo() {
  // return request(`http://192.168.190.85/wxtc-service/api/produces/${id}`, {
  return request(window.appcfg._servicesUrl + `/api/produces`, {
    method: 'GET',
  });
}

// 获取疫情防控的数据
export async function getEpidemicData(projectId, isEmergency) {
  const q = new URLSearchParams();
  q.append('emphasisFlag', isEmergency ? 1 : 0);
  if (projectId) {
    q.append('projectId', projectId);
  }

  return request(`${window.appcfg.zdgcService}/api/wuxi/api/majorProject/getEpPersonalInfo?${q}`, {
    method: 'GET',
  });
}

export async function getProjSafetyIndex() {
  return request(`${window.appcfg.zdgcService}/api/wuxi/api/majorProject/getSafeIndex`, {
    method: 'GET',
  });
}

export async function getProjectProgressStat() {
  return request(
    `${window.appcfg.zdgcService}/api/wuxi/api/majorProject/emphasisProject/projectProgress`,
    {
      method: 'GET',
    },
  );
}

export async function getCompanyProjCount() {
  return request(`${window.appcfg.zdgcService}/api/wuxi/api/project/companys`, {
    method: 'GET',
  });
}
